package com.briup.smartcity.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.briup.smartcity.bean.Article;
import com.briup.smartcity.bean.Category;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.CategoryMapper;
import com.briup.smartcity.mapper.extend.ArticleExtendMapper;
import com.briup.smartcity.mapper.extend.CategoryExtendMapper;
import com.briup.smartcity.service.ICategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class ICategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryExtendMapper extendMapper;
    @Autowired
    private ArticleExtendMapper articleExtendMapper;

    @Override
    public PageInfo<Category> pageQueryAll(int pageNum, int pageSize) throws ServiceException {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> allCategory = extendMapper.findAllCategory();
        PageInfo pageInfo=new PageInfo(allCategory);
        return pageInfo;
    }

    @Override
    public void deleteById(int id) throws ServiceException {
//        List<Category> l = extendMapper.findAllCategory();
        int[] ids = extendMapper.findIds();
        boolean contains = ArrayUtil.contains(ids, id);
        if (contains==false) {
            throw new ServiceException("该栏目已被删除,请刷新页面");
        }
        categoryMapper.deleteByPrimaryKey(id);//service->dao
    }

    @Override
    public void batchDelete(int[] ids) throws ServiceException {
        extendMapper.deleteAllCategoryById(ids);//ids为web层拿到的数组
    }

    @Override
    public void saveCategory(Category category) throws ServiceException {
        //两个都相同则插入失败
        //查询数据库
        Integer[] ee=extendMapper.findAllOrder();
        Integer[] pa=extendMapper.findAllParent();
        //获取用户输入
        Integer ord=category.getCategoryParentId();
        System.out.println(ord);
        Integer order = category.getCategoryOrder();
        //判断
        boolean flag1=ArrayUtil.contains(pa,ord);
        boolean flag=ArrayUtil.contains(ee,order);
        if (order==0){
            throw new ServiceException("序号不能为0或者空值");
        }
        if (category.getCategoryDescription()==null){
            System.out.println("sd");
            throw new ServiceException("请输入栏目描述");
        }
        if (flag==true&&flag1==true){
            throw new ServiceException("该父栏目下已有该排序号，请重新输入！");
        }
        categoryMapper.insertSelective(category);
    }

    @Override
    public void updateCategory(Category category) throws ServiceException {
        if (category.getCategoryOrder()==0){
            throw new ServiceException("序号不能为0或者空值");
        }
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public List<Category> findAll() throws ServiceException {
        List<Category> allCategory = extendMapper.findAllCategory();
        return allCategory;
    }

    @Override
    public List<Category> findEqual() throws ServiceException {
        return extendMapper.selectAllEqualById();
    }

    @Override
    public List<Article> findAllArticle() throws ServiceException {
        return articleExtendMapper.findArticle();
    }



}
